package cn.itcast.bos.action.base;

import java.util.HashMap;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.IStandardService;
@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
/**
 * 派取标准
 * @author hds
 *
 */
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	
	private static final long serialVersionUID = 670868321359731716L;
	@Autowired
	private IStandardService standardService;
	
	// 模型驱动获取standard对象参数
	private Standard standard = new Standard();
	
	private int page;
	private int rows;
	
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	/**
	 * 保存取派标准
	 * @return
	 */
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String standardSave(){
		standardService.save(standard);
		return SUCCESS;
	}
	@Action(value="standard_pagequery",results={@Result(name="success",type="json")})
	public String standardQuery(){
		Pageable pageable = new PageRequest(page-1,rows);
		Page<Standard> result = standardService.pageQuery(pageable);
		// 格式化数据
		HashMap<String, Object> map = new HashMap<>();
		map.put("total", result.getTotalElements());	//总数据条数
		map.put("rows", result.getContent());			//每页显示的真实数据
		// 数据压入值栈顶端
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	@Override
	public Standard getModel() {
		return standard;
	}

}
